let COS = require('../../utils/cos-wx-sdk-v5.js')
Page({
    data: {
        imageList: [
            // "/static/images/tabbar/ic_menu_choice_nor.png"
        ],
    },
    uploadImage() {
        let that = this;
        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                // 默认图片 + 选择的图片； 
                that.setData({
                    imageList: that.data.imageList.concat(res.tempFilePaths)
                });
            }
        });
    },
    uploadFile() {
        var onlineImageList = [];

        // var cos = new COS({
        //     SecretId: 'AKIDFLnASxLYdWDieRPHzZ5PXb2RfojV4OYV',
        //     SecretKey: 'sWzYv63JOGvOioiPnBUdifbMxZ1INIdy',
        // });

        var cos = new COS({
            getAuthorization: function (options, callback) {
                // 服务端 JS 和 PHP 示例：https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
                // 服务端其他语言参考 COS STS SDK ：https://github.com/tencentyun/qcloud-cos-sts-sdk
                // STS 详细文档指引看：https://cloud.tencent.com/document/product/436/14048
                wx.request({
                    url: 'http://hknps.888u.ml:8080/api/credential',
                    data: {
                        // 可从 options 取需要的参数
                    },
                    success: function (result) {
                        var data = result.data;
                        var credentials = data.credentials;
                        callback({
                            TmpSecretId: credentials.tmpSecretId,
                            TmpSecretKey: credentials.tmpSecretKey,
                            XCosSecurityToken: credentials.sessionToken,
                            ExpiredTime: data.expiredTime,
                        });
                    }
                });
            }
        });

        for (var index in this.data.imageList) {
            var filePath = this.data.imageList[index];
            cos.postObject({
                Bucket: 'mini-1252695631',
                Region: 'ap-nanjing',
                Key: index + "uuu.png",
                FilePath: filePath,
                onProgress: function (info) {
                    console.log('进度条', JSON.stringify(info));
                }
            }, function (err, data) {
                // data.Location是图片上传成功后，人家返回的地址
                onlineImageList.push(data.Location);
                console.log(onlineImageList);
            });
        }

    },
    onLoad(options) {

    },
})